using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEditor.UI;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    [SerializeField] private float thrust; // 推力
    private Rigidbody rb;
    [SerializeField] private GameObject bulletPrefab;
    [SerializeField] private GameObject firePoint;
    [SerializeField] private GameObject shield;
    [SerializeField] private TMP_Text scoreText;
    [HideInInspector] public GameInput _input;
    private Coroutine _shieldCoroutine;
    [SerializeField] private GameObject liveContainer;
    private int _lives = 3;
    private int _score;

    public int Lives
    {
        get => _lives;
        set
        {
            _lives = value;
            UpdateLives();
        }
    }
    public int Score
    {
        get => _score;
        set
        {
            _score = value;
            scoreText.text = _score.ToString();
        }
    }
    void Awake()
    {
        _input = new GameInput();
        _input.Enable();
    }
    void Start()
    {
        UpdateLives();
        Score = 0;
        rb = GetComponent<Rigidbody>();
        EventManager.OnScore += ScoreHandler;
    }

    void Update()
    {
        // Vector3 mousePos = Input.mousePosition; 
        // mousePos.z = 10f;
        // Vector3 screenPoint = Camera.main.ScreenToWorldPoint(mousePos);
        // this.transform.LookAt(screenPoint, Vector3.back);
        
        Vector3 mousePos =
            Camera.main.ScreenToWorldPoint(new Vector3(_input.KeyBroadandMouse.ScreenPos.ReadValue<Vector2>().x, _input.KeyBroadandMouse.ScreenPos.ReadValue<Vector2>().y, 10.0f));
        transform.LookAt(mousePos, Vector3.back);

        Vector2 movement = _input.KeyBroadandMouse.Move.ReadValue<Vector2>();
        
        rb.AddForce(movement * thrust * Time.deltaTime, ForceMode.Acceleration);


        if (_input.KeyBroadandMouse.Shoot.triggered)
        {
            // GameObject bullet = Instantiate(bulletPrefab);
            // bullet.transform.LookAt(screenPoint, Vector3.up);
            
            Quaternion lookRotation = Quaternion.LookRotation(transform.forward);
            Instantiate(bulletPrefab, firePoint.transform.position, lookRotation);
            AudioManager.instance.PlayMusic(4);
            //Destroy(bullet, 3f);
        }
    }

    private void UpdateLives()
    {
        for (int i = 0; i < 5; i++)
        {
            liveContainer.transform.GetChild(i).gameObject.SetActive(false);
        }
        for (int i = 0; i < Lives; i++)
        {
            liveContainer.transform.GetChild(i).gameObject.SetActive(true);
        }


    }

    private void OnTriggerEnter(Collider other)
    {
        switch (other.tag)
        {
            case "ShieldPowerUp":{
                Destroy(other.gameObject);
                shield.SetActive(true);
                if (_shieldCoroutine!=null)
                {
                    StopCoroutine(_shieldCoroutine);
                }
                _shieldCoroutine= StartCoroutine(ShieldCoroutine());
                AudioManager.instance.PlayMusic(0);
                break;
            }
            case "LifePowerUp":{
                Destroy(other.gameObject);
                if (Lives<5)
                {
                    Lives++;
                }
                AudioManager.instance.PlayMusic(0);
                break;
            }
            case "Rock":
            case "LittleRock":
            {
                if (shield.activeSelf)
                {
                    Destroy(other.gameObject);
                    break;
                }
                else
                {
                    Lives--;
                    Destroy(other.gameObject);
                    AudioManager.instance.PlayMusic(3);
                    if (Lives<=0)
                    {
                        print("Game Over");
                    }
                    break;
                }
            }
        }
    }

    IEnumerator ShieldCoroutine()
    {
        yield return new WaitForSeconds(10f);
        shield.SetActive(false);
    }
    
    private void ScoreHandler(int score)
    {
        Score += score;
    }

    private void OnDisable()
    {
        _input.Disable();
        EventManager.OnScore -= ScoreHandler;
    }
    
    
}